﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLLottery.Controls
{
    public partial class OnlineStatusControl : UserControl
    {
        public static readonly DependencyProperty OnlineStatusProperty = DependencyProperty.Register("OnlineStatus", typeof(short), typeof(OnlineStatusControl), new PropertyMetadata(new PropertyChangedCallback(OnOnlineStatusChanged)));
        Storyboard story;
        public OnlineStatusControl()
        {
            InitializeComponent();
            story = new Storyboard();
            ColorAnimation animation1 = new ColorAnimation();
            Storyboard.SetTarget(animation1, ((RadialGradientBrush)this.ellipse.Fill).GradientStops[0]);
            Storyboard.SetTargetProperty(
               animation1, new PropertyPath(GradientStop.ColorProperty));
            animation1.Duration = new Duration(new TimeSpan(0, 0, 1)); ;
            animation1.To = Colors.White;
            story.Stop();
            story.Children.Add(animation1);

            ColorAnimation animation2 = new ColorAnimation();
            Storyboard.SetTarget(animation2, ((RadialGradientBrush)this.ellipse.Fill).GradientStops[1]);
            Storyboard.SetTargetProperty(
               animation2, new PropertyPath(GradientStop.ColorProperty));
            animation2.Duration = new Duration(new TimeSpan(0, 0, 1)); ;
            animation2.To = Common.MyColor.ConvertColor("#ff2ADC16");
            story.Stop();
            story.Children.Add(animation2);

            story.AutoReverse = true;
            story.RepeatBehavior = RepeatBehavior.Forever;
            this.LayoutRoot.Resources.Add("sotry_", story);

            OnlineStatus = 1;
        }

        void OnlineStatusControl_Loaded(object sender, RoutedEventArgs e)
        {
          
        }
        private static void OnOnlineStatusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as OnlineStatusControl).SetOnlineStatus((short)e.NewValue);
        }

        public void SetOnlineStatus(short onlinestatus)
        {
            if (onlinestatus == 1)
            {
                this.a.Color = Color.FromArgb(0xff, 0, 0xff, 0);
                this.b.Color = Colors.Green;
                story.Begin();
            }
            else if (onlinestatus == 0)
            {
                this.a.Color = Colors.White;
                this.b.Color = Colors.Gray;
                story.Stop();
            }
            else
            {
                LayoutRoot.Visibility = Visibility.Collapsed;
            }
        }

        public short OnlineStatus
        {
            get
            {
                return (short)GetValue(OnlineStatusProperty);
            }
            set
            {
                SetValue(OnlineStatusProperty, value);
            }
        }
    }
}
